﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Forms;

namespace InteractiveKidsShow
{
    class ButtonDecisionPart : StoryPart
    {
        public int vidLength;
        public List<InteractButton> buttons;
        public bool hasVidPlayed;
        public bool hasSecondVidPlayed;
        public int buttonChosen;

        public ButtonDecisionPart(Uri requestVid, int requestVidLength, List<InteractButton> buttons)
        {
            vid = requestVid;
            vidLength = requestVidLength;
            this.buttons = buttons;
            hasVidPlayed = false;
            hasSecondVidPlayed = false;
            buttonChosen = -1;
        }


        public void Play(ref MediaElement mediaPlayer, ref Kinect kinectObject)
        {
            if (!hasVidPlayed) // if the first vid (request) hasn't played, play that
            {
                mediaPlayer.Source = vid;
                mediaPlayer.Play();
                hasVidPlayed = true;
                foreach (InteractButton aButton in buttons)
                {
                    kinectObject.addActiveButton(aButton.buttonName, aButton.coordinate1, aButton.coordinate2, aButton.coordinate3, aButton.coordinate4);
                }
                kinectObject.requestButtonAnswer(vidLength);
            }
            else // otherwise it has been played
            {
                if (buttonChosen < 0) // if no button has been chosen
                {
                        hasSecondVidPlayed = true;
                        mediaPlayer.Source = buttons[0].vid;
                        mediaPlayer.Play();
                }
                else //otherwise, a button has been chosen
                {
                    try // play the video corresonding to that button
                    {
                        hasSecondVidPlayed = true;
                        mediaPlayer.Source = buttons[buttonChosen].vid;
                        mediaPlayer.Play();
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        MessageBox.Show("ERROR: Probably from trying to play the video for a button that doesn't exist, somehow. Nice going. OFFICIALLY:" + e.Message);
                    }
                }
            }

        }
    }
}
